package com.dingup.provider.service.log.impl;

import com.alibaba.fastjson.JSON;
import com.dingup.api.model.log.SysRoleUpdateLog;
import com.dingup.api.model.sys.SysPermissionGroup;
import com.dingup.provider.mapper.SysPermissionGroupMapper;
import com.dingup.provider.mapper.SysRoleUpdateLogMapper;
import com.dingup.provider.model.sys.UpdateRolePermission;
import com.dingup.provider.service.log.RoleUpdateLogService;
import com.dingup.provider.service.sys.SysPermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by wancheng on 2018/8/10.
 */
@Service
public class RoleUpdateLogServiceImpl implements RoleUpdateLogService{

    private static Logger logger = LoggerFactory.getLogger(RoleUpdateLogServiceImpl.class);

    private final String SAVE_CONTENT = "创建了该身份";

    private final String ADD_CONTENT = " 添加了 ";

    private final String DELETE_CONTENT = " 删除了 ";

    private final String END = "的操作权限";

    private final String ROLE_ADD = "ROLE_ADD";

    private final String ROLE_DELETE = "ROLE_DELETE";

    private final String DELETE = "DELETE";

    private final String ADD = "ADD";

    @Autowired
    SysRoleUpdateLogMapper sysRoleUpdateLogMapper;
    @Autowired
    SysPermissionService sysPermissionService;
    @Autowired
    SysPermissionGroupMapper sysPermissionGroupMapper;

    @Override
    public void saveRoleLog(Integer roleId, String userId) {
        SysRoleUpdateLog log = new SysRoleUpdateLog();
        log.setContent(SAVE_CONTENT);
        log.setCreateTime(new Date());
        log.setCreateUserId(userId);
        log.setRoleId(roleId);
        log.setType(ADD);
        sysRoleUpdateLogMapper.insertSelective(log);
    }

    @Override
    public void updateRoleLog(Integer roleId, String userId, String permissionGroup) {
        List<UpdateRolePermission> list = JSON.parseArray(permissionGroup,UpdateRolePermission.class);
        StringBuffer content = new StringBuffer("");
        //修改后的权限Id
        SysRoleUpdateLog log = new SysRoleUpdateLog();
        list.stream().forEach(updateRolePermission -> {
            if (updateRolePermission.getIfHasPermission()) {
                content.append(ADD_CONTENT);
                SysPermissionGroup g = sysPermissionGroupMapper.selectByPrimaryKey(updateRolePermission.getId());
                content.append(g.getGroupName()+" ");
                log.setType(ROLE_ADD);
            }else{
                content.append(DELETE_CONTENT);
                SysPermissionGroup g = sysPermissionGroupMapper.selectByPrimaryKey(updateRolePermission.getId());
                content.append(g.getGroupName()+" ");
                log.setType(ROLE_DELETE);
            }
        });
        content.append(END);
        log.setContent(content.toString());
        log.setCreateTime(new Date());
        log.setCreateUserId(userId);
        log.setRoleId(roleId);

        sysRoleUpdateLogMapper.insertSelective(log);

    }

    @Override
    public void deleteRoleLog(Integer roleId, String userId,String roleName) {
        SysRoleUpdateLog log = new SysRoleUpdateLog();
        log.setContent("删除了 "+roleName+" 身份");
        log.setCreateTime(new Date());
        log.setCreateUserId(userId);
        log.setRoleId(roleId);
        log.setType(DELETE);
        sysRoleUpdateLogMapper.insertSelective(log);
    }

    @Override
    public List<SysRoleUpdateLog> getUpdateLog(Integer roleId) {
        return sysRoleUpdateLogMapper.getUpdateRoleLog(roleId,true);
    }

    @Override
    public List<SysRoleUpdateLog> getDeleteLog() {
        return sysRoleUpdateLogMapper.getUpdateRoleLog(null,false);
    }
}
